<?php
/**
 * Created by PhpStorm.
 * User: fenfen
 * Date: 2016/11/17
 * Time: 10:45
 */

namespace backend\controllers;

use bc\controller\BackendController;
use bc\model\wc\StaffModel;
use bc\model\wc\HospitalModel;
use bc\model\UserModel;
use bc\util\backend\Page;

class StaffController extends BackendController
{
    public function actionIndex()
    {
        $real_name = $this->get('real_name', '');
        $where =  'bc_wc_staff.is_invalid = 1';

        if(!empty($real_name)){
            $where = $where.' and bc_user.real_name like \'%'.$real_name.'%\'';
//            $where['bc_user.real_name']='%'.$real_name.'%';
        }

        $page = $this->get('page', 1);
        $sort = 'id desc';
        $doctorM = new StaffModel ();
        $doctorM->columns='bc_wc_staff.id as s_id,user_id,hospital_id,phone,bc_user.id as u_id,real_name,nick_name,sex,wxid,bc_wc_hospital.id as h_id,address,name';
        $doctorList = $doctorM->getStaffList ($where,$page,self::PAGE_SIZE,$sort);

        $total = $doctorM->getTotal ( $where );
        $pagination = new Page ( $total, self::PAGE_SIZE );

        $row['pagination'] = $pagination;
        $row ['doctorList'] = $doctorList;
        $row ['real_name'] = $real_name;
        return $this->render('index',$row);
    }

    /*
     * 编辑
     * */
    public function actionEdit()
    {
        $id = $this->get('id');
        $where=array('bc_wc_staff.id'=>$id);
        $doctorM = new StaffModel ();
        $doctorM->columns='bc_wc_staff.id as s_id,user_id,hospital_id,phone,bc_user.id as u_id,real_name,nick_name,sex,wxid,bc_wc_hospital.id as h_id,address,name';
        $doctorList = $doctorM->getStaffList ($where);
        $data['row']= $doctorList[0];
        return $this->render('edit',$data);

    }

    /*
 * 保存
 * */
    public function actionSave()
    {
        $id = $this->get('id');
        $data_u['real_name'] = $this->get('real_name');
        $data_u['sex'] = $this->get('sex');
        $uid = $this->get('uid');
        $h_id=$this->get('h_id');
        $usermodel = new UserModel();
        $usermodel->modify($data_u, $uid);

        $data['phone'] = $this->get('phone');
        $model = new StaffModel();
        if(empty($id))
        {
            $data['create_time']=time();
            $data['update_time']=time();
            $data['hospital_id'] = $h_id;
            $data['is_invalid']=1;
            $model->add($data);
        }else {
            $data['update_time']=time();
            $data['hospital_id'] = $h_id;
            $model->modify($data, $id);
        }
        $this->redirect('/staff/index');
    }

    /*
     * 删除
     * */
    public function actionDelet()
    {
        $id = $this->get('id');
        $model = new StaffModel();
        $data['update_time']=time();
        $data['is_invalid']=0;
        $model->modify($data, $id);
        $this->redirect('/staff/index');
    }

    /*
     * 添加
     * */
    public function actionAdd()
    {
        return $this->render('add');
    }
}